package ru.spb.osll.myfit.server.execute;

import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.isA;
import static org.easymock.EasyMock.same;
import static org.junit.Assert.assertEquals;

import org.easymock.EasyMock;
import org.easymock.EasyMockSupport;
import org.easymock.IAnswer;
import org.junit.Before;
import org.junit.Test;

import fit.FixtureLoader;
import fitnesse.wiki.PageCrawler;
import fitnesse.wiki.PageData;
import fitnesse.wiki.WikiPage;
import fitnesse.wiki.WikiPagePath;

public class FitNesseTestRunnerTest extends EasyMockSupport {

	private class TestableFitNesseTestRunner extends FitNesseTestRunner {
				public TestableFitNesseTestRunner(String rootPath,
				String rootDirectoryName, FixtureLoader fixtureLoader,
				ProgressFixtureListener listener, WikiPage root) throws Exception {
			super(rootPath, rootDirectoryName, fixtureLoader, listener);
			m_root = root;
		}
	}

	private static final String HTML_PAGE = "<table border=\"1\" cellspacing=\"0\">"
			+ "<tr><td>Import</td></tr>"
			+ "<tr><td>ru.spb.osll.myfit.fixtures.base</td>"
			+ "</tr></table>"
			+ "<table border=\"1\" cellspacing=\"0\">"
			+ "<tr><td class=\"pass\">dummy one line</td>"
			+ "<td>OK</td></tr></table>";

	private static final String RESULT_HTML = "<table border=\"1\" cellspacing=\"0\">"
			+ "<tr><td>Import</td></tr>"
			+ "<tr><td>ru.spb.osll.myfit.fixtures.base</td></tr>"
			+ "</table>"
			+ "<table border=\"1\" cellspacing=\"0\">"
			+ "<tr><td class=\"pass\" class=\"pass\">dummy one line</td><td>OK</td></tr>"
			+ "</table>";

	private static final String ERROR_RESULT_HTML = "<table border=\"1\" cellspacing=\"0\">"
			+ "<tr><td>Import</td></tr>"
			+ "<tr><td>ru.spb.osll.myfit.fixtures.base</td></tr>"
			+ "</table>"
			+ "<table border=\"1\" cellspacing=\"0\" class=\"error\">"
			+ "<tr><td class=\"pass\">dummy one line</td><td>OUPS</td></tr>"
			+ "</table>";

	private FitNesseTestRunner testRunner;
	private WikiPage root;
	private PageCrawler crawler;
	private WikiPage page;
	private PageData data;
	private MyFitFixtureListener listener;

	@Before
	public void setUp() {
		root = createStrictMock(WikiPage.class);
		crawler = createStrictMock(PageCrawler.class);
		page = createStrictMock(WikiPage.class);
		data = createStrictMock(PageData.class);
		listener = new MyFitFixtureListener();
	}

	@Test
	public void testExecuteTest() throws Exception {
		expect(root.getPageCrawler()).andReturn(crawler);
		expect(crawler.getPage(same(root), isA(WikiPagePath.class))).andAnswer(
				new IAnswer<WikiPage>() {
					@Override
					public WikiPage answer() throws Throwable {
						assertEquals("TestSuite.TestPage", EasyMock
								.getCurrentArguments()[1].toString());
						return page;
					}
				});

		expect(page.getData()).andReturn(data);
		expect(data.getHtml()).andReturn(HTML_PAGE);

		replayAll();
		testRunner = new TestableFitNesseTestRunner("path/to/fitnesse",
				"FitNesseRoot", FixtureLoader.instance(), listener, root);
		testRunner.run("TestSuite.TestPage");
		String result = listener.getOutput().replace("&nbsp;", "");
		verifyAll();
		assertEquals(RESULT_HTML, result);
	}

	@Test
	public void testExecuteTestWithException() throws Exception {
		expect(root.getPageCrawler()).andReturn(crawler);
		expect(crawler.getPage(same(root), isA(WikiPagePath.class))).andAnswer(
				new IAnswer<WikiPage>() {
					@Override
					public WikiPage answer() throws Throwable {
						assertEquals("TestSuite.TestPage2", EasyMock
								.getCurrentArguments()[1].toString());
						return page;
					}
				});

		expect(page.getData()).andReturn(data);
		expect(data.getHtml()).andReturn(HTML_PAGE.replaceAll("OK", "OUPS"));

		replayAll();
		testRunner = new TestableFitNesseTestRunner("path/to/fitnesse",
				"FitNesseRoot", FixtureLoader.instance(), listener, root);
		testRunner.run("TestSuite.TestPage2");
		String result = listener.getOutput().replace("&nbsp;", "");
		verifyAll();
		assertEquals(ERROR_RESULT_HTML, result);
	}
	
}
